<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$_SESSION['onemerchid']=0;
	$return['setting']=$return['pluginset'];
	$title=$return['pluginset']['share_title']?:$return['pluginset']['title'];
	$desc=$return['pluginset']['share_desc'];
	$logo=$return['pluginset']['share_image'];
	$return['member']=cfc('core')->get_member($_SESSION['uid']);
	$params=array('mid'=>0,'merchid'=>0,'uniacid'=>$_SESSION['uniacid']);
	$params['parentid']=intval($_RQ['id']);
	$category=pdo_getall('jy_weishop_goods_group',$params,'','id','paixu desc');
	$return['category']=$category;
	$sharelink='jy_tiffany/index//ismerch=1&merchid=0&fromuser='.$_SESSION['uid'];
	$return['sharedata']=array(
			'title'=>$title?:'新品购物商城',
			'desc'=>$desc?:'新品购物商城！',
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($logo),
	);
	$return['list']=array();
	$return['pcd']=array();
	$return['params']['pid']=intval($_RQ['pid']);
	$return['time']=time();
	$return['shopset']=plugin('jy_weishop')->plugin_setting('shopset');
	if(intval($this->setting['indextpl'])){
		$return['htmls']=$this->template_url('index_a1');
		$return['pageset']=$this->plugin_setting('page');
		$hdps=array();
		foreach($return['pageset']['hdps5'] as $hv){
			$hv['key']=5;
			$hdps[]=$hv;
		}
		foreach($return['pageset']['hdps1'] as $hv){
			$hv['key']=1;
			$hdps[]=$hv;
		}
		foreach($return['pageset']['hdps2'] as $hv){
			$hv['key']=2;
			$hdps[]=$hv;
		}
		foreach($return['pageset']['hdps4'] as $hv){
			$hv['key']=4;
			$hdps[]=$hv;
		}
		foreach($return['pageset']['hdps3'] as $hv){
			$hv['key']=3;
			$hdps[]=$hv;
		}
		$return['nowhdp']=$return['pageset']['hdps2'][0];
		$return['nowhdp']['key']=2;
		$return['nowkey']=count($return['pageset']['hdps5'])+count($return['pageset']['hdps1']);
		$return['pageset']['hdps']=$hdps;
		$return['pageset']['newsgroups3_1title']=pdo_getcolumn('core_account_article_fenlei',array('id'=>$return['pageset']['newsgroups3_1'],'uniacid'=>$_SESSION['uniacid']),'title');
		$return['pageset']['newsgroups3_2title']=pdo_getcolumn('core_account_article_fenlei',array('id'=>$return['pageset']['newsgroups3_2'],'uniacid'=>$_SESSION['uniacid']),'title');
		$return['pageset']['newsgroups3_3title']=pdo_getcolumn('core_account_article_fenlei',array('id'=>$return['pageset']['newsgroups3_3'],'uniacid'=>$_SESSION['uniacid']),'title');
		$return['pageset']['newsgroups3_4title']=pdo_getcolumn('core_account_article_fenlei',array('id'=>$return['pageset']['newsgroups3_4'],'uniacid'=>$_SESSION['uniacid']),'title');
		$return['pageset']['newsgroups5_1title']=pdo_getcolumn('core_account_article_fenlei',array('id'=>$return['pageset']['newsgroups5_1'],'uniacid'=>$_SESSION['uniacid']),'title');
		$return['pageset']['newsgroups5_2title']=pdo_getcolumn('core_account_article_fenlei',array('id'=>$return['pageset']['newsgroups5_2'],'uniacid'=>$_SESSION['uniacid']),'title');
		$return['pageset']['newsgroups5_3title']=pdo_getcolumn('core_account_article_fenlei',array('id'=>$return['pageset']['newsgroups5_3'],'uniacid'=>$_SESSION['uniacid']),'title');
		$return['pageset']['newsgroups5_4title']=pdo_getcolumn('core_account_article_fenlei',array('id'=>$return['pageset']['newsgroups5_4'],'uniacid'=>$_SESSION['uniacid']),'title');
		$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
		$params['plugin_name OR']='plugin_name=\'jy_weishop\' or plugin_name=\'\' or plugin_name=\''.$_SESSION['current_plugin'].'\'';
		$fields=array('id','title','content','fenlei','keyword','createtime','readnum','likenum','status','desc','pic_url','yingxiao');
		$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
		$pindex = max(1, intval($_RQ['page']));
		$psize = $return['pageset']['newsgroups3_1num']?:10;
		//页面1
		if($return['pageset']['newsgroups1']){
			$params['fenlei']=str_array($return['pageset']['newsgroups1']);
		}
		if($return['pageset']['newsgroups1num']){
			$psize=$return['pageset']['newsgroups1num'];
		}
		$return['pageset']['listnews1']=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
		foreach($return['pageset']['listnews1'] as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,56);
			$item['pic_url'] = tomedia($item['pic_url']);
			$item['createtime']=date('Y-m-d',$item['createtime']);
		}
		$psize = $return['pageset']['newsgroups3_1num']?:10;
		//页面3
		$params['fenlei']=$return['pageset']['newsgroups3_1']?:-1;
		$return['pageset']['newsgroups3_1list']=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
		foreach($return['pageset']['newsgroups3_1list'] as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,56);
			$item['pic_url'] = tomedia($item['pic_url']);
			$item['createtime']=date('Y-m-d',$item['createtime']);
		}
		$params['fenlei']=$return['pageset']['newsgroups3_2']?:-1;
		$psize = $return['pageset']['newsgroups3_2num']?:10;
		$return['pageset']['newsgroups3_2list']=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
		foreach($return['pageset']['newsgroups3_2list'] as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,56);
			$item['pic_url'] = tomedia($item['pic_url']);
			$lasttime=TIMESTAMP-$item['createtime'];
			if($lasttime>30*24*3600){
				$item['createtime']=intval($lasttime/(30*24*3600)).'月前';
			}elseif($lasttime>24*3600){
				$item['createtime']=intval($lasttime/(24*3600)).'天前';
			}elseif($lasttime>=3600){
				$item['createtime']=intval($lasttime/3600).'小时前';
			}else{
				$item['createtime']=intval($lasttime/60).'分钟前';
			}
		}
		$params['fenlei']=$return['pageset']['newsgroups3_3']?:-1;
		$psize = $return['pageset']['newsgroups3_3num']?:10;
		$return['pageset']['newsgroups3_3list']=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
		foreach($return['pageset']['newsgroups3_3list'] as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,56);
			$item['pic_url'] = tomedia($item['pic_url']);
			$item['createtime']=date('Y-m-d',$item['createtime']);
		}
		$params['fenlei']=$return['pageset']['newsgroups3_4']?:-1;
		$psize = $return['pageset']['newsgroups3_4num']?:10;
		$return['pageset']['newsgroups3_4list']=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
		foreach($return['pageset']['newsgroups3_4list'] as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,56);
			$item['pic_url'] = tomedia($item['pic_url']);
			$item['createtime']=date('Y-m-d',$item['createtime']);
		}
		//页面5
		$params['fenlei']=$return['pageset']['newsgroups5_1']?:-1;
		$psize = $return['pageset']['newsgroups5_1num']?:10;
		$return['pageset']['newsgroups5_1list']=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
		foreach($return['pageset']['newsgroups5_1list'] as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,56);
			$item['pic_url'] = tomedia($item['pic_url']);
			$item['createtime']=date('Y-m-d',$item['createtime']);
		}
		$params['fenlei']=$return['pageset']['newsgroups5_2']?:-1;
		$psize = $return['pageset']['newsgroups5_2num']?:10;
		$return['pageset']['newsgroups5_2list']=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
		foreach($return['pageset']['newsgroups5_2list'] as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,56);
			$item['pic_url'] = tomedia($item['pic_url']);
			$lasttime=TIMESTAMP-$item['createtime'];
			if($lasttime>30*24*3600){
				$item['createtime']=intval($lasttime/(30*24*3600)).'月前';
			}elseif($lasttime>24*3600){
				$item['createtime']=intval($lasttime/(24*3600)).'天前';
			}elseif($lasttime>=3600){
				$item['createtime']=intval($lasttime/3600).'小时前';
			}else{
				$item['createtime']=intval($lasttime/60).'分钟前';
			}
		}
		$params['fenlei']=$return['pageset']['newsgroups5_3']?:-1;
		$psize = $return['pageset']['newsgroups5_3num']?:10;
		$return['pageset']['newsgroups5_3list']=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
		foreach($return['pageset']['newsgroups5_3list'] as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,56);
			$item['pic_url'] = tomedia($item['pic_url']);
			$item['createtime']=date('Y-m-d',$item['createtime']);
		}
		$params['fenlei']=$return['pageset']['newsgroups5_4']?:-1;
		$psize = $return['pageset']['newsgroups5_4num']?:10;
		$return['pageset']['newsgroups5_4list']=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
		foreach($return['pageset']['newsgroups5_4list'] as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,56);
			$item['pic_url'] = tomedia($item['pic_url']);
			$item['createtime']=date('Y-m-d',$item['createtime']);
		}
		$title=$return['pageset']['title2'];
	}
	set_html('title',$title?:'新品购物商城');
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'nochecked'=>0,
		'no_showsys'=>0,
		'type <>'=>4,
		'onepluginname'=>'',
	);
	if($_SESSION['onemerchid']){
		$params['merchid']=$_SESSION['onemerchid'];
		unset($params['no_showsys']);
		unset($params['onepluginname']);
	}
	if($_RQ['cid']){
		$params['ccate']=intval($_RQ['cid']);
		$return['params']['ccate']=intval($_RQ['cid']);
	}	
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if($_RQ['pid']){
		$params['pcate']=intval($_RQ['pid']);
		$return['params']['pid']=intval($_RQ['pid']);
	}else{
		$return['params']['pid']=0;
	}
	if($_RQ['order']=='isrecommand'){
		$params['saletype likes']=array('1');
		$return['params']['order']='isrecommand';
	}
	if($_RQ['order']=='isnew'){
		$params['saletype likes']=array('2');
		$return['params']['order']='isnew';
	}
	if($_RQ['order']=='ishot'){
		$params['saletype likes']=array('3');
		$return['params']['order']='ishot';
	}
	if($_RQ['priceMin']){
		$params['price >=']=intval($_RQ['priceMin']);
		$return['params']['priceMin']=intval($_RQ['priceMin']);
	}
	if($_RQ['priceMax']){
		$params['price <=']=intval($_RQ['priceMax']);
		$return['params']['priceMax']=intval($_RQ['priceMax']);
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','image','price','active_plugin','active_id','salenum','originalprice','usermaxbuy','unit','api_data');
	$orderby='paixu desc, star desc, id desc ';
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if(empty($list)){
		exi('none');
	}else{
		foreach($list as $k=>&$itemv){
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['jianprice']=$itemv['originalprice']-$itemv['price'];
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			//按点赞数排序作品
			$pindex = max(1, 0);
			$psize = 4;
			$members=pdo_getslice('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),array($pindex,$psize),$total,array('mid'),'','id desc');
			foreach($members as &$mv){
				$memberinfo=pdo_get('core_members',array('id'=>$mv['mid']),array('nickname','avatar'));
				$mv['nickname']=$memberinfo['nickname'];
				$mv['avatar']=$memberinfo['avatar'];
			}
			$itemv['members']=$members;			
			$itemv['merchname']=$myshop['merchname'];
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],),'id');
			$cartinfo=pdo_get('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']));
			$cartinfo['total']=pdo_getsum('jy_weishop_member_cart',array('selected'=>1,'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
			$itemv['cartid']=$cartinfo['id']?:0;
			$itemv['carttotal']=$cartinfo['total']?:0;
			$itemv['cartprice']=$cartinfo['price']?:0;
			$itemv['cartoptionid']=$cartinfo['optionid']?:0;
			$itemv['cartid']=$itemv['cartid']?:0;
			$itemv['carttotal']=$itemv['carttotal']?:0;
			$itemv['specs_list']=array();
			if($_RQ['is_specs'] && $itemv['optionid']){
				//规格
				$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$itemv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specs_list){
						foreach($specs_list as $k=>&$sp){
							$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						}
				}
				//已选规格
				$selectspecs = array();
				$selespename = '';
				$specs = pdo_getcolumn('jy_weishop_goods_option',array('id'=>$itemv['optionid'],'goodsid'=>$itemv['id']),'specs');				
				$selectspecs = str_array($specs,'_');
				if($selectspecs){
						foreach($selectspecs as $k=>&$spid){
							$title=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spid,'uniacid'=>$_SESSION['uniacid']),'title');
							$selespename .=$title.'，';
						}
				}
				//默认已选参数：
				$itemv['selespename'] = $selespename;
				$itemv['specs_list'] = $specs_list;
				$itemv['specid']=$selectspecs;
				$cartnum=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$itemv['options']['id'],'goodsid'=>$itemv['id'],'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
				$itemv['post'][$itemv['options']['id']]=array('num'=>$cartnum?:0,'title'=>$selespename);
			}
			if($_SESSION['is_tomedia']){
				$itemv['image']=$itemv['image']?tomedia($itemv['image']):'';
				$itemv['video']=$itemv['video']?tomedia($itemv['video']):'';
			}
			if($_RQ['wxappjiaoyi']){
				$itemv['id']=$itemv['api_id'];
			}
			$itemv['api_data']=$itemv['api_data']?json_decode($itemv['api_data'],true):array();
		}
	}
	exi($list);
}
?>